---
--类名: SystemUI.lua
--创建日期: 2015-4-1
--创建人: 陈小虎
--功能描述: 系统ui管理
--修改内容：
--修改日期:
--修改人:
--修改备注:
--

local SystemUI = class("SystemUI",function()
    return display.newNode()
end)

-- 创建BlurDialog
function SystemUI:_createBlurBg()
    if cc.isDead( self._blurBg ) then -- 为空，或者被销毁？
        -- release_print( " ========================>>>>>> SystemUI:_createBlurBg " )

        self._blurBg = display.newBlurDialog()
        self:addChild( self._blurBg, -1 )
    end
end
-- 销毁BlurDialog
function SystemUI:_clearBlurBg()
    if not cc.isDead( self._blurBg ) then -- 不为空 且 存活？
        -- release_print( " ========================>>>>>> SystemUI:_clearBlurBg " )

        self._blurBg:removeFromParent( true )
        self._blurBg = nil
    end
end
-- 设置BlurDialog可见性
function SystemUI:_setBlurBgVisible( b )
    if not cc.isDead( self._blurBg ) then
        -- release_print( " ========================>>>>>> SystemUI:_setBlurBgVisible ", b )
        self._blurBg:setVisible( b )
    end
end

function SystemUI:ctor()
    self:setName("SystemUI")
    self._uiKeys = {}

    self._allUiKeysList = {} -- [ i, uikey ] 记录打开过的UI顺序（ 位置i <-> uikey ）
    self._keyIdx = {} -- [ uikey, i ] uikey处于_allUiKeysList中位置
end

function SystemUI:isHaveSystemUI()
    -- return table.nums(self._uiKeys) > 0
    return #self._allUiKeysList > 0
end

function SystemUI:getUIByKey(uiKey)
    return self:getChildByName(uiKey)
end

-- 设置清空回调
function SystemUI:setEmptyCB(cb)
    self._emptyCB = cb
end

-- 检查是否有子ui
function SystemUI:checkEmpty()
    if not self:isHaveSystemUI() then
        self:_clearBlurBg()
        executeFunc(self._emptyCB)
    end
end

function SystemUI:addSystemUI(uiKey,uiNode,notDelete,isFade)
    print("SystemUI:addSystemUI uiKey", uiKey)
    if not cc.isDead(uiNode) and not cc.isDead(self) then
        local newAddedNode = false
        if not self._keyIdx[uiKey] then -- UI不存在？
            newAddedNode = true

            self:addChild(uiNode)
            uiNode:setName(uiKey)
        end

        -- 加UI时是否需要显示BlurDialog
        local visibleTemp = false
        if uiNode.getViewType then
            visibleTemp = app.mvc.VIEWTYPE_BLUR == uiNode:getViewType()
        end
        -- 打开此UI之前没有其他UI存在
        if visibleTemp and cc.isDead( self._blurBg ) then
            self:_createBlurBg()
        end
        self:_setBlurBgVisible( visibleTemp )

        -- print( " -------------------->>>>>>> 打开 ", uiKey )
        -- 缓存进UI列表
        if not newAddedNode then -- 此次打开的是旧节点？
            self:_adjustUIListWhenRemove( uiKey )
        end
        -- 插入打开过的UI列表
        table.insert( self._allUiKeysList, uiKey )
        self._keyIdx[uiKey] = #self._allUiKeysList

        -- dump( self._allUiKeysList , " ---------->>>>>>> _allUiKeysListI ", 10 )
        -- dump( self._keyIdx , " ---------->>>>>>> _keyIdxI ", 10 )

        -- 以下：隐藏前一个打开的系统UI
        local oldNodeName = self._allUiKeysList[#self._allUiKeysList-1] -- 前一个打开的UI名字
        -- print( " -------------------->>>>>>> 隐藏UiName ", oldNodeName )
        if oldNodeName then
            local oldNode = self:getChildByName( oldNodeName )
            if not cc.isDead( oldNode ) then
                oldNode:setVisible( false )
            end
        end
        -- 显示当前操作的UI
        uiNode:setVisible( true )
    end

    if uiKey and not notDelete then
        self._uiKeys[uiKey] = uiKey
    end
end

-- 帮助函数：调整打开过的UI列表
function SystemUI:_adjustUIListWhenRemove( uiKey )
    local idx = self._keyIdx[uiKey]
    self._keyIdx[uiKey] = nil
    if idx then
        -- print( " -------------------->>>>>>> 调整位置 ",idx )

        table.remove( self._allUiKeysList, idx )
        for i = idx+1, #self._allUiKeysList+1 do
            local adjustUiName = self._allUiKeysList[ i-1 ]
            local oldIdx = self._keyIdx[ adjustUiName ]

            -- print( string.format( " ----------->>>>>> %s, %s, 原位置:%s ==>> 新位置:%s", i, adjustUiName, oldIdx, oldIdx - 1 ) )
            self._keyIdx[ adjustUiName ] = oldIdx - 1
        end
    end
end


-- 帮助函数：移除UI时候调整UI列表
function SystemUI:_handlerUIListWhenRemove( uiKey )
    -- print( " -------------------->>>>>>> 关闭 ",uiKey )
    self:_adjustUIListWhenRemove( uiKey )

    -- 以下：恢复前一个打开的系统UI可见
    local curNodeName = self._allUiKeysList[#self._allUiKeysList]
    if curNodeName then
        local curNode = self:getChildByName( curNodeName )
        if not cc.isDead( curNode ) then
            curNode:setVisible( true )

            if curNode.getViewType then
                self:_setBlurBgVisible( app.mvc.VIEWTYPE_BLUR == curNode:getViewType() )
            end

            if curNode.reOpen then
                curNode:reOpen()
            end
        end
    end

    -- dump( self._allUiKeysList , " ---------->>>>>>> _allUiKeysListO ", 10 )
    -- dump( self._keyIdx , " ---------->>>>>>> _keyIdxO ", 10 )

    if 0 >= #self._allUiKeysList then -- 不存在系统UI了？
        -- print( " ---------------------------------- 不存在系统UI了？" )
        self._allUiKeysList = {}
        self._keyIdx = {}
    end
end

function SystemUI:removeSystemUiByKey(uiKey)
    print("SystemUI:removeSystemUiByKey uiKey", uiKey)
    if not uiKey then return end
    if not self:getUIByKey(uiKey) then return end
    
    self:removeChildByName(uiKey)

    self._uiKeys[uiKey] = nil
    self:_handlerUIListWhenRemove( uiKey )

    self:isClearBlurBg()
    self:checkEmpty()
end

function SystemUI:removeAllUI()
    local isHaveSystemUI = self:isHaveSystemUI()

    for k,uiKey in pairs(self._uiKeys) do
        self:removeChildByName(uiKey)
        self:_handlerUIListWhenRemove( uiKey )
    end
    self._uiKeys = {}

    if isHaveSystemUI then
        self:checkEmpty()
    end
end

-- 移除一个UI就要判断剩下所有的UI是否还有需要显示遮罩的，没有就调清空回调
function SystemUI:isClearBlurBg()   
    local isClearFlag = true
    for i, v in ipairs(self._allUiKeysList) do
        local curNode = self:getChildByName(v)
        if not cc.isDead(curNode) and app.mvc.VIEWTYPE_BLUR == curNode:getViewType() then
            isClearFlag = false
            break
        end
    end
    if isClearFlag then
        self:_clearBlurBg()
        executeFunc(self._emptyCB)
    end
end

return SystemUI
